package com.shu.executors;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2020/9/1 11:41
 */
@Slf4j
public class ThreadPoolStateTest {


    public static void main(String[] args) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(5);

        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("自定义线程池A" + "-%d")
                .setDaemon(true)
                .build();

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 4,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                threadFactory);


        LongAdder longAdder = new LongAdder();
        Runnable runnable = () -> {
            longAdder.increment();
            int arg = longAdder.intValue();
            log.info("开始执行{}", arg);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("---------------结束执行{}--------------", arg);
            countDownLatch.countDown();
        };

        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);
        threadPoolExecutor.execute(runnable);

        countDownLatch.await();


        log.info("over");


//        threadPoolExecutor.shutdownNow();
//        ThreadPoolExecutor.isRunning(threadPoolExecutor.ctrl.get());

    }
}
